package com.neusoft.weekreport.component.bizService.impl;

import com.neusoft.weekreport.component.bizService.LoginBizService;
import com.neusoft.weekreport.component.entities.MsUser;
import com.neusoft.weekreport.component.entities.MsUserExample;
import com.neusoft.weekreport.component.req.LoginReq;
import com.neusoft.weekreport.component.resp.LoginResp;
import com.neusoft.weekreport.mapper.MsUserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LoginBizServiceImpl implements LoginBizService {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    MsUserMapper msUserMapper;

    @Override
    public LoginResp getMsUser(LoginReq req) {
        LoginResp loginResp = new LoginResp();
        try {
            MsUserExample msUserExample = new MsUserExample();
            MsUserExample.Criteria criteria = msUserExample.createCriteria();
            criteria.andAccountNameEqualTo(req.getAccountname());
            criteria.andEnableEqualTo(1);
            List<MsUser> msUsers = msUserMapper.selectByExample(msUserExample);
            if(msUsers.size()==0){
                loginResp.setMsg("账号不存在，请注册！");
            }else{
                criteria.andPasswordEqualTo(req.getPassword());
                List<MsUser> msUsers1 = msUserMapper.selectByExample(msUserExample);
                if(msUsers1.size()==0){
                    loginResp.setMsg("密码不正确，请重新输入");
                }else{
                    loginResp.setId(msUsers1.get(0).getId());
                    loginResp.setAccountname(msUsers1.get(0).getAccountName());
                    loginResp.setUsername(msUsers1.get(0).getUserName());
                    loginResp.setPassword(msUsers1.get(0).getPassword());
                }
            }
        } catch (Exception e) {
            logger.error("ms_user用户表查询出错",e);
        }

        return loginResp;
    }


}
